package com.daxia.bumall.web.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.bumall.dto.ApiModuleDTO;
import com.daxia.bumall.dto.ApiTestDTO;
import com.daxia.bumall.dto.JsonResultDTO;
import com.daxia.bumall.model.ApiTestParameter;
import com.daxia.bumall.service.ApiTestService;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/apiTest", produces="text/html;charset=UTF-8")
public class AdminApiTestController extends BaseController {

	@Autowired
	private ApiTestService apiTestService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			ApiTestDTO dto = apiTestService.load(id);
			String url = "/" + dto.getUrl();
			/*
	        UrlResponseDTO ur = urlResponseService.findByUrl(url);
	        if (ur != null && ur.getStatus() == 0) {
	            map.put("updateUrlResponse", true);
	        } else {
	            map.put("updateUrlResponse", false);
	        } */
			map.put("n", dto);
		}
		List<ApiModuleDTO> apiModules =  apiModuleService.find(new ApiModuleDTO(), null);
		map.put("apiModules", apiModules);
		return "admin/apiTest/apiTest_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	
	public String save(ApiTestDTO dto, JsonResultDTO resultDTO, String[] names, String[] requires, String[] defaultValues, String[] descriptions) throws Exception {
		List<ApiTestParameter> parameters = new ArrayList<ApiTestParameter>();
        if (ArrayUtils.isNotEmpty(names)) {
            for (int i = 0; i < names.length; i++) {
                ApiTestParameter parameter = new ApiTestParameter();
                parameter.setName(names[i]);
                parameter.setRequired("1".equals(requires[i]));
                parameter.setDefaultValue(ArrayUtils.isEmpty(defaultValues) ? null : defaultValues[i]);
                parameter.setDescription(ArrayUtils.isEmpty(descriptions) ? null : descriptions[i]);
                parameters.add(parameter);
            }
        }
		if (dto.getId() == null) {
			apiTestService.create(dto, parameters);
		} else {
			apiTestService.updateAllFields(dto, parameters);
		}
		return okAndClose(resultDTO);
	}

    
	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		apiTestService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	
	public String list(ApiTestDTO dto, Map<String, Object> map, Page page) {
		List<ApiTestDTO> dtos = apiTestService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		
		List<ApiModuleDTO> apiModules =  apiModuleService.find(new ApiModuleDTO(), null);
		map.put("apiModules", apiModules);
		
		return "admin/apiTest/apiTest_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/datagrid")
    
    public String datagrid(ApiTestDTO dto, Map<String, Object> map, Page page) throws Exception {
        List<ApiTestDTO> dtos = apiTestService.find(dto, page);
        JSONArray jsonArray = new JSONArray();
        for (ApiTestDTO apiTest : dtos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", apiTest.getId());
            jsonObject.put("name", apiTest.getName());
            jsonObject.put("description", apiTest.getDescription());
            jsonObject.put("url", apiTest.getUrl());
	        jsonObject.put("apiModule.name", apiTest.getApiModule().getName());    	
            jsonObject.put("exampleUrl", apiTest.getExampleUrl());
            jsonObject.put("exampleResponse", apiTest.getExampleResponse());
            jsonObject.put("requestMethod", apiTest.getRequestMethod());
            jsonObject.put("status", apiTest.getStatus());
            jsonObject.put("responseRemark", apiTest.getResponseRemark());
            jsonArray.add(jsonObject);
        }
        JSONObject json = new JSONObject();
        json.put("total", page.getTotalRecords());
        json.put("rows", jsonArray);
        return json.toJSONString();
    }
	
}
